<?php

/**
 * This is the model class for table "{{article}}".
 *
 * The followings are the available columns in table '{{article}}':
 * @property string $id
 * @property string $title
 * @property string $name
 * @property string $summary
 * @property string $content
 * @property integer $display
 * @property integer $hot
 * @property string $categoryid
 * @property string $seriesid
 * @property integer $localeid
 * @property string $updatetime
 * @property string $updateuserid
 * @property string $createtime
 * @property string $createuserid
 *
 * The followings are the available model relations:
 * @property Category $category
 * @property Series $series
 * @property Locale $locale
 */
class Article extends ActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Article the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{article}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title, name, summary, content, categoryid, localeid', 'required'),
			array('display, hot, localeid', 'numerical', 'integerOnly'=>true),
			array('title, name', 'length', 'max'=>255),
			array('summary', 'length', 'max'=>1024),
			array('categoryid, seriesid, updateuserid, createuserid', 'length', 'max'=>11),
			array('updatetime, updateuserid, createuserid, createtime', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, title, name, summary, content, display, hot, categoryid, seriesid, localeid, updatetime, updateuserid, createtime, createuserid', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'category' => array(self::BELONGS_TO, 'Category', 'categoryid'),
			'series' => array(self::BELONGS_TO, 'Series', 'seriesid'),
			'locale' => array(self::BELONGS_TO, 'Locale', 'localeid'),
                        'uploads' => array(self::MANY_MANY, 'Upload', '{{upload_article}}(articleid,uploadid)'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'title' => 'Title',
			'name' => 'Name',
			'summary' => 'Summary',
			'content' => 'Content',
			'display' => 'Display',
			'hot' => 'Hot',
                        'uploads' => 'Upload',
			'categoryid' => 'Categoryid',
			'seriesid' => 'Seriesid',
			'localeid' => 'Localeid',
			'updatetime' => 'Updatetime',
			'updateuserid' => 'Updateuserid',
			'createtime' => 'Createtime',
			'createuserid' => 'Createuserid',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('summary',$this->summary,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('display',$this->display);
		$criteria->compare('hot',$this->hot);
		$criteria->compare('categoryid',$this->categoryid,true);
		$criteria->compare('seriesid',$this->seriesid,true);
		$criteria->compare('localeid',$this->localeid);
		$criteria->compare('updatetime',$this->updatetime,true);
		$criteria->compare('updateuserid',$this->updateuserid,true);
		$criteria->compare('createtime',$this->createtime,true);
		$criteria->compare('createuserid',$this->createuserid,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        public function getCategoryOptions()
        {
            return CHtml::listData(Category::model()->findAll(), 'id', 'title');
        }
        
        public function getSeriesOptions()
        {
            return CHtml::listData(Series::model()->findAll(), 'id', 'title');
        }
        
        public function getLocaleOptions()
        {
            return CHtml::listData(Locale::model()->findAll(), 'id', 'desc');
        }
}